#include<thread>
#include<iostream>
#include<condition_variable>
#include<mutex>


//为了更好展示流程,输出函数过程
template <class lock_t>
class debugmutex 
{
public:
lock_t m;
void lock()
{   
    std::cout<<"lock()"<<std::endl;
    m.lock();
}

bool try_lock()
{
    std::cout<<"try_lock()"<<std::endl;
    return m.try_lock();
}

template <class rep>
bool try_lock_until(rep t)
{
    std::cout<<"try_lock_until()"<<std::endl;
    return m.try_lock_until(t);
}

template <class rep>
bool try_lock_for(rep t)
{
    std::cout<<"try_lock_for()"<<std::endl;
    return m.try_lock_for(t);
}

void unlock()
{   
     std::cout<<"unlock()"<<std::endl;
    m.unlock();
}
};



int main(int n,char**)
{
    using std::cout,std::endl,std::thread,std::condition_variable
         ,std::unique_lock,std::mutex,std::cin,std::string;
    
    //对象清单
    const int workernum =10;
    thread workerThs[workernum];
    std::condition_variable_any cv;
    debugmutex<std::timed_mutex> m;
    auto workerf=[&m,&cv]()
    {   
        
        unique_lock uk(m);
        cout<<"等待中 "<<endl;
        cv.wait(uk);
        cout<<std::this_thread::get_id()<<":开始"<<endl;

    };
    
    //开始test
    for(auto &e:workerThs){e=thread(workerf);}
    //发信号
    string ch;
    while (cin>>ch)
    {
    if(ch=="1")
    cv.notify_one();
    if(ch=="all")
        cv.notify_all();
    if(ch=="quit")
    break;
    }
    //等待汇合
    for(auto &e:workerThs){e.join();}
}

